<template>
	<view class="wrap">
		<head-nav-bar title="WMS" />
		<view class="logo">仓库管理</view>
		<div v-if="loginType === 'currentPhone'">
			
			<!-- <u-tabs :list="list" :is-scroll="false" :current="current" @change="onClickItem"></u-tabs> -->
			<!-- <view v-if="current === 0"> -->
			<!-- {{baseUrl}} -->
			<view class="list">
				<view class="list-call">
					<view class="iconfont icon-yonghu" style="font-size: 22px;color:#5473e8;"></view>
					<input class="u-input" type="text" v-model="username" maxlength="32"
						:placeholder="$t('login.placeholderAccount')" value="admin" />
				</view>
				<view class="list-call">
					<view class="iconfont icon-lock-fill" style="font-size: 22px;color:#5473e8;"></view>
					<input class="u-input" type="text" v-model="password" maxlength="32"
						:placeholder="$t('login.placeholderPassword')" :password="!showPassword" value="admin123" />
					<image class="u-icon-right"
						:src="'/static/aidex/login/eye_' + (showPassword ? 'open' : 'close') + '.png'"
						@click="showPass()"></image>
				</view>
				<!-- <view class="list-call">
					令牌
					<input class="u-input" type="text" v-model="myToken" clear
						 />
				</view> -->
				<view class="list-call">
					<view class="" style="font-size: 22px;color:#5473e8;">令牌:</view>
					<u-input class="u-input ip-text" type="text" v-model="myToken" maxlength="32"
						placeholder="" value="admin" clear />
				</view>
				<div style="padding:15rpx 0 0;margin-top: 34rpx;">
					<!-- <view class="register">
						<navigator class="register-link" url="forget" open-type="navigate">{{$t('login.forget')}}</navigator>
						<navigator class="register-link" url="reg" open-type="navigate">{{$t('login.reg')}}</navigator>
					</view> -->
					<u-checkbox v-model="savePassword" active-color="#5473e8">保存密码</u-checkbox>
					<u-checkbox v-model="remember" active-color="#5473e8">{{$t('login.autoLogin')}}</u-checkbox>

				</div>
			</view>
			<view class="button" @click="login('1')"><text>虚拟登录</text></view>
			<view class="button" @click="submit('1')"><text>登录</text></view>
			<u-divider>{{baseUrl}}</u-divider>
		</div>



	</view>
</template>
<script>
	/**
	 * Copyright (c) 2013-Now http://aidex.vip All rights reserved.
	 */
	import base64 from '@/common/base64.js';
	import HeadNavBar from '@/components/headnavbar/index';
	export default {
		components: {
			HeadNavBar
		},
		data() {
			return {
				phoneNo: '',
				username: 'admin',
				password: 'rKu1/348LvKp0rsVC06eCA==',
				myToken:'Bearer c6ec9383-a31a-4c04-a6d9-fe29bb6eef74',
				loginType: 'currentPhone',
				showPassword: false,
				savePassword: true,
				remember: true,
				isValidCodeLogin: false,
				validCode: '',
				imgValidCodeSrc: null,
				list: [{
					name: '用户名'
				}, {
					name: '手机号'
				}],
				baseUrl:'',
				current: 0,
				activeColor: '#007aff',
			};
		},
		created() {
			
		},
		onLoad(options) {
			// debugger
			if(options.reload){
				console.log("应该重载的啊")
				this.$forceUpdate()
			}
			console.log("onload运行")
			this.baseUrl = uni.getStorageSync('baseUrl')
			if(!this.baseUrl){
				this.$u.toast('ip为空,请先设置ip');
			}
			this.init()
		},
		methods: {
			init(){
				//监听Ip修改事件
				var _this = this
				// uni.$on("baseUrl", async (res) => {
				// 	console.log("登录界面监听到的async:" + res); //我是全局事件订阅的调用方法
				// 	_this.baseUrl = res;
				// 	uni.setStorageSync('baseUrl',res)
				// 	this.$router.go(0)
				// })
				try {
					
					console.log("vuex_token:"+this.vuex_token)
					let remember = uni.getStorageSync('remember');
					const savePassword = uni.getStorageSync('savePassword');
					
					
					const username = uni.getStorageSync('username');
					const password = uni.getStorageSync('password');
					const logout = uni.getStorageSync('logout');
					console.log("缓存中的值:remember==>" + remember);
					console.log("缓存中的值:username==>" + username);
					console.log("缓存中的值:password==>" + password);
					console.log("缓存中的值:savePassword==>" + savePassword);
					console.log("缓存中的值:logout==>" + logout);
					const ROLE_PDA = uni.getStorageSync('ROLE_PDA');
					console.log("缓存中的值:ROLE_PDA==>" + ROLE_PDA);
					const USER_GH = uni.getStorageSync('USER_GH');
					console.log("缓存中的值:USER_GH==>" + USER_GH);
					const OWNER_RIGHT = uni.getStorageSync('OWNER_RIGHT');
					console.log("缓存中的值:OWNER_RIGHT==>" + OWNER_RIGHT);
					const USER_STATUS = uni.getStorageSync('USER_STATUS');
					console.log("缓存中的值:USER_STATUS==>" + USER_STATUS);
					const WH_RIGHT = uni.getStorageSync('WH_RIGHT');
					console.log("缓存中的值:WH_RIGHT==>" + WH_RIGHT);
					const USER_ID = uni.getStorageSync('USER_ID');
					console.log("缓存中的值:USER_ID==>" + USER_ID);
					const USER_NAME = uni.getStorageSync('USER_NAME');
					console.log("缓存中的值:USER_NAME==>" + USER_NAME);
					// uni.$u.vuex('vuex_authorization', 'Bearer 4278f705-8a62-4f28-9328-9e97bf7febdf')
					
					// console.log("缓存中的值:vuex_authorization==>" + uni.$u.vuex('vuex_authorization'));
					
					
					
					
					// 用户默认保存
					// this.username=username?username:'8888'
				
					// this.savePassword = JSON.parse(savePassword)
					this.username = username ? username : this.username
					// this.password='8888'
					// 密码允许保存,再显示,不允许,则不允许显示了
					if (this.savePassword) {
						this.password = password ? password : this.password
					}
					if (logout) {
						// console.log("ip:"+this.baseUrl)
						console.log("ip:"+this.vuex_config.baseUrl)
						console.log("退出登录,不要自动登录")
					} else {
						if (JSON.parse(remember)) {
							console.log("打开app过来的")
							this.remember = true
							this.submit("1")
							// 先别急着跳转,看一下是不是'退出登录'过来的
						} else {
							this.remember = false
						}
					}
					
				} catch (e) {
				}
			},
			getSessionStorage(){
				this.$u.toast(sessionStorage.getItem('hello'))
			},
			showPass() {
				this.showPassword = !this.showPassword;
			},
			qiehuanLogin() {
				this.loginType = 'other'
			},
			onClickItem(index) {
				this.current = index;
			},
			refreshImgValidCode(e) {
				// if (this.vuex_token == '') {
				// 	this.$u.api.index().then(res => {
				// 		this.imgValidCodeSrc = this.vuex_config.baseUrl + '/validCode?__sid=' +
				// 			res.sessionid + '&t=' + new Date().getTime();
				// 	});
				// } else {
				// 	this.imgValidCodeSrc = this.vuex_config.baseUrl + '/validCode?__sid=' +
				// 		this.vuex_token + '&t=' + new Date().getTime();
				// }
				// this.validCode = '';
			},
			nextStep() {
				//验证码登录下一步
				uni.showLoading({
					title: '正在获取验证码',
					mask: true
				})
				this.$u.api.sendCode({
						phoneNo: this.phoneNo,
						validCodeType: '2'
					})
					.then(res => {
						if (res.code == '200') {
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/sys/login/code?phoneNo=' + this.phoneNo
								});
							}, 500);
						} else {
							this.$u.toast(res.msg);
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/sys/login/code?phoneNo=' + this.phoneNo
								});
							}, 500);
						}
					});
			},
			// 虚拟登录,不验证用户名密码,直接登录成功
			login(){
				uni.setStorageSync('remember', JSON.stringify(this.remember));
				uni.setStorageSync('savePassword', JSON.stringify(this.savePassword));
				// 直接先跳过注册
				uni.setStorageSync('baseService', true);
				uni.setStorageSync('registered_time','2022-01-01/9031-01-01')
				this.$u.toast('登录成功');
				this.$u.vuex('vuex_authorization', this.myToken);
				console.log('vuex_authorization:'+this.vuex_authorization)
				setTimeout(() => {
							uni.reLaunch({
								url: '/pages/sys/workbench/wms-work'
							});
					}, 500);
			},
			submit(loginType) {
				const isCanUse =uni.getStorageSync('isCanUse');
				console.log("isCanUse:"+isCanUse)
				if (this.username.length == 0) {
					this.$u.toast('请输入账号');
					return;
				}
				if (this.password.length == 0) {
					this.$u.toast('请输入密码');
					return;
				}
		
				this.$u.api.my_login({
						username: this.username,
						password: this.password,
						grant_type: 'password',
						scope: 'server'
					})
					.then(res => {
						console.log("返回的res:"+JSON.stringify(res))
						if(!res){
							this.$u.toast("请求无效,请查看ip")
							return;
						}
						let data = res.data
						// data = JSON.parse(data)
						
						data=data[0]
						console.log("返回的data:"+JSON.stringify(data))
						if (res.msg) {
							// this.$u.toast(res.msg);
						}
						// if (res.code == "200") {
						if (res.success) {
							//,wms特殊字段返回
							uni.setStorageSync('ROLE_PDA', data.ROLE_PDA);
							uni.setStorageSync('USER_GH', data.USER_GH);
							uni.setStorageSync('OWNER_RIGHT', data.OWNER_RIGHT);
							uni.setStorageSync('USER_STATUS', data.USER_STATUS);
							uni.setStorageSync('WH_RIGHT', data.WH_RIGHT);
							uni.setStorageSync('USER_ID', data.USER_ID);
							uni.setStorageSync('USER_NAME', data.USER_NAME);
							// 登录成功的用户,默认帮用户记忆一下哈
							uni.setStorageSync('username', this.username);
							uni.setStorageSync('logout', false);
							
							try {
								uni.setStorageSync('remember', JSON.stringify(this.remember));
								uni.setStorageSync('savePassword', JSON.stringify(this.savePassword));
							} catch (e) {
								// error
							}
							// 由选项而进行的记忆
							if (this.savePassword) {
								uni.setStorageSync('password', this.password);
							} else {
								uni.setStorageSync('password', '');
							}

							const ROLE_PDA = uni.getStorageSync('ROLE_PDA');
							console.log("缓存中的值:ROLE_PDA==>" + ROLE_PDA);
							const USER_GH = uni.getStorageSync('USER_GH');
							console.log("缓存中的值:USER_GH==>" + USER_GH);
							const OWNER_RIGHT = uni.getStorageSync('OWNER_RIGHT');
							console.log("缓存中的值:OWNER_RIGHT==>" + OWNER_RIGHT);
							const USER_STATUS = uni.getStorageSync('USER_STATUS');
							console.log("缓存中的值:USER_STATUS==>" + USER_STATUS);
							const WH_RIGHT = uni.getStorageSync('WH_RIGHT');
							console.log("缓存中的值:WH_RIGHT==>" + WH_RIGHT);
							const USER_ID = uni.getStorageSync('USER_ID');
							console.log("缓存中的值:USER_ID==>" + USER_ID);
							const USER_NAME = uni.getStorageSync('USER_NAME');
							console.log("缓存中的值:USER_NAME==>" + USER_NAME);
							this.getStorage()
							
							if (process.env.NODE_ENV === 'development') {
								console.log('开发环境');
							} else {
								console.log('生产环境');
							}

							// 直接先跳过注册
							uni.setStorageSync('baseService', true);
							uni.setStorageSync('registered_time','2022-01-01/9031-01-01')


						}else{
							this.$u.toast(res.msg);
						}
						if (res.isValidCodeLogin) {
							this.isValidCodeLogin = true;
							this.refreshImgValidCode();
						}
					});
			},
			getStorage() {
				const user_id = uni.getStorageSync('USER_ID')
				let param = user_id;
				console.log(param)
				this.$u.api.general({
						iv_code: "USERSTORAGEROLE",
						iv_parm: param
					})
					.then(res => {
						console.log("返回的res:" + JSON.stringify(res))
						let data = res.data
						console.log("返回的data:" + JSON.stringify(data))
						if (res.success) {
							//补充text
							 const storageList = data.map(v => {
								this.$set(v, 'text', v
									.STORAGE_NAME) // https://vuejs.org/v2/guide/reactivity.html
								return v
							})
							uni.setStorageSync("storageList",JSON.stringify(storageList))
						} else {
							this.$u.toast(res.msg);
						}
						this.actionSheetList  = JSON.parse(uni.getStorageSync("storageList"))
						if(this.actionSheetList.length>0){
							setTimeout(() => {
										uni.reLaunch({
											url: '/pages/sys/workbench/wms-work'
										});
								}, 500);
						}else{
							this.$u.toast('跳转失败:没有获取到该仓库人员的所属仓库列表,请联系管理员');
							return;
						}
						console.log("缓存中的值:storageList==>" + uni.getStorageSync("storageList"));
						// setTimeout(() => {
						// 		let storageList=uni.getStorageSync("storageList")
						// 		if(storageList.length>0){
						// 			uni.reLaunch({
						// 				url: '/pages/sys/workbench/wms-work'
						// 			});
						// 		}else{
						// 			this.$u.toast('没有获取到该仓库人员的所属仓库列表,请联系管理员');
						// 			return
						// 		}
								
						// 	}, 500);
					});
			},
			wxLogin(res) {
				this.$u.toast('微信登录');
			},
			qqLogin() {
				this.$u.toast('QQ 登录');
			}
		}
	};
</script>
<style lang="scss">
	@import 'index.scss';

	.logo {
		width: 80%;
		font-size: 64rpx;
		color: #5473e8;
		margin: 80rpx auto 80rpx auto;
	}

	.list-call-icon {
		color: #ff0000;
	}

	.currentPhone-box {
		text-align: center;
		padding: 40rpx 80rpx;

		.number-text {
			color: #000000;
			font-size: 60rpx;
		}

		.other-text {
			color: #999999;
			font-size: 26rpx;
			padding: 20rpx 0;
		}

		.u-btn {
			margin: 30rpx auto;
		}

		.u-hairline-border {
			border: 1px solid #fff;
		}
	}

	.register {
		display: inline-block;
		color: #5473e8;
		height: 40rpx;
		line-height: 40rpx;
		font-size: 28rpx;
		float: right;
		margin-top: 6rpx;
	}

	.register-link {
		float: right;
		padding: 0 16rpx;
	}

	.reg-link {
		display: inline-block;
		color: #5473e8;
	}

	.oauth2 {
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		margin: 0rpx 100rpx 30rpx;

		image {
			height: 80rpx;
			width: 80rpx;
		}
	}

	.u-tabs {
		padding: 0 70rpx;
	}
</style>
